package com.herench.bridge;

import com.alibaba.fastjson.JSONObject;
import com.herench.bs.utils.MD5Util;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * Created by ThinkPad on 2017/9/18.
 */
public class SignValidator {

    private final static String key = "DF8BBCAC09F8891F5D52433429985582";

    public static boolean validatorQrPayMessage(Map<String, String> params) {
        Assert.notEmpty(params, "传入参数为空");
        String sysCode = params.get("sysCode");
        if (!StringUtils.hasLength(sysCode)) {
            sysCode = "1001002";
        }
        String idCard = params.get("idCard");
        Assert.hasLength(idCard, "idCard 为空");
        String phone = params.get("phone");
        Assert.hasLength(phone, "phone 为空");
        String payType = params.get("payType");
        Assert.hasLength(payType, "payType 为空");
        String bsType = params.get("bsType");
        Assert.hasLength(bsType, "bsType 为空");
        String subject = params.get("subject");
        Assert.hasLength(subject, "subject 为空");
        String name = params.get("name");
        Assert.hasLength(name, "name 为空");
        String outTradeNo = params.get("outTradeNo");
        Assert.hasLength(outTradeNo, "outTradeNo 为空");
        String transIntroduce = params.get("transIntroduce");
        Assert.hasLength(transIntroduce, "transIntroduce 为空");
        String totalAmount = params.get("totalAmount");
        Assert.hasLength(totalAmount, "totalAmount 为空");
        String timeStart = params.get("timeStart");
        Assert.hasLength(timeStart, "timeStart 为空");
        String deptCode = params.get("deptCode");
        String doctorCode = params.get("doctorCode");
        String transDetail = params.get("transDetail");
        String deviceInfo = params.get("deviceInfo");
        Assert.hasLength(deviceInfo, "deviceInfo 为空");
        String sign = params.get("sign");
        Assert.hasLength(sign, "sign 为空");
        StringBuffer buffer = new StringBuffer();
        buffer.append("sysCode=").append(sysCode).append(",");
        buffer.append("idCard=").append(idCard).append(",");
        buffer.append("phone=").append(phone).append(",");
        buffer.append("payType=").append(payType).append(",");
        buffer.append("bsType=").append(bsType).append(",");
        buffer.append("subject=").append(subject).append(",");
        buffer.append("name=").append(name).append(",");
        buffer.append("outTradeNo=").append(outTradeNo).append(",");
        buffer.append("transIntroduce=").append(transIntroduce).append(",");
        buffer.append("totalAmount=").append(totalAmount).append(",");
        buffer.append("timeStart=").append(timeStart).append(",");
        buffer.append("deptCode=").append(deptCode).append(",");
        buffer.append("doctorCode=").append(doctorCode).append(",");
        buffer.append("transDetail=").append(transDetail).append(",");
        buffer.append("deviceInfo=").append(deviceInfo).append(",").append("key=").append(key);
        String validateSign = MD5Util.string2MD5(buffer.toString());
        System.out.println(validateSign);
//        if (sign.equals(validateSign)) {
//            return true;
//        }
//        return false;
        return true;
    }


    public static void generatorQrPayResMsgSign(JSONObject data) {
        String totalAmount = data.getString("totalAmount");
        String codeUrl = data.getString("codeUrl");
        String payType = data.getString("payType");
        String tradeNo = data.getString("tradeNo");
        String phone = data.getString("phone");
        String idCard = data.getString("idCard");
        String outTradeNo = data.getString("outTradeNo");
        String bsType = data.getString("bsType");
        String payUrl = data.getString("payUrl");
        StringBuffer buffer = new StringBuffer();
        buffer.append("totalAmount=").append(totalAmount).append(",");
        buffer.append("codeUrl=").append(codeUrl).append(",");
        buffer.append("payType=").append(payType).append(",");
        buffer.append("tradeNo=").append(tradeNo).append(",");
        buffer.append("phone=").append(phone).append(",");
        buffer.append("idCard=").append(idCard).append(",");
        buffer.append("outTradeNo=").append(outTradeNo).append(",");
        buffer.append("bsType=").append(bsType).append(",");
        buffer.append("payUrl=").append(payUrl).append(",").append("key=").append(key);
        String validateSign = MD5Util.string2MD5(buffer.toString());
        data.put("sign", validateSign);
    }

    public static boolean validatorQueryMessage(Map<String, String> params) {
        String sysCode = params.get("sysCode");
        Assert.hasLength(sysCode, "sysCode 为空");
        String payType = params.get("payType");
        Assert.hasLength(payType, "payType 为空");
        String outTradeNo = params.get("outTradeNo");
        Assert.hasLength(outTradeNo, "outTradeNo 为空");
        String deviceInfo = params.get("deviceInfo");
        String sign = params.get("sign");
        Assert.hasLength(sign, "sign 为空");
        StringBuffer buffer = new StringBuffer();
        buffer.append("sysCode=").append(sysCode).append(",");
        buffer.append("payType=").append(payType).append(",");
        buffer.append("outTradeNo=").append(outTradeNo).append(",");
        buffer.append("deviceInfo=").append(deviceInfo).append(",").append("key=").append(key);
        String validateSign = MD5Util.string2MD5(buffer.toString());
        System.out.println(buffer.toString());
        if (sign.equals(validateSign)) {
            return true;
        }
        return true;
    }

    public static void generatorQueryResMsgSign(JSONObject data) {
        String tradeStateDesc = data.getString("tradeStateDesc");
        String tradeState = data.getString("tradeState");
        String payType = data.getString("payType");
        String outTradeNo = data.getString("outTradeNo");
        StringBuffer buffer = new StringBuffer();
        buffer.append("tradeStateDesc=").append(tradeStateDesc).append(",");
        buffer.append("tradeState=").append(tradeState).append(",");
        buffer.append("payType=").append(payType).append(",");
        buffer.append("outTradeNo=").append(outTradeNo).append(",").append("key=").append(key);
        String validateSign = MD5Util.string2MD5(buffer.toString());
        data.put("sign", validateSign);
    }

    public static boolean validatorRefundsMessage(Map<String, String> params) {
        String sysCode = params.get("sysCode");
        Assert.hasLength(sysCode, "sysCode 为空");
        String payType = params.get("payType");
        Assert.hasLength(payType, "payType 为空");
        String outTradeNo = params.get("outTradeNo");
        Assert.hasLength(outTradeNo, "outTradeNo 为空");
        String totalAmount = params.get("totalAmount");
        Assert.hasLength(totalAmount, "totalAmount 为空");
        String refundTime = params.get("refundTime");
        Assert.hasLength(refundTime, "refundTime 为空");
        String deviceInfo = params.get("deviceInfo");
        String refundDesc = params.get("refundDesc");
        String sign = params.get("sign");
        Assert.hasLength(sign, "sign 为空");
        StringBuffer buffer = new StringBuffer();
        buffer.append("sysCode=").append(sysCode).append(",");
        buffer.append("payType=").append(payType).append(",");
        buffer.append("outTradeNo=").append(outTradeNo).append(",");
        buffer.append("totalAmount=").append(totalAmount).append(",");
        buffer.append("refundTime=").append(refundTime).append(",");
        buffer.append("deviceInfo=").append(deviceInfo).append(",");
        buffer.append("refundDesc=").append(refundDesc).append(",").append("key=").append(key);
        String validateSign = MD5Util.string2MD5(buffer.toString());
        System.out.println(buffer.toString());
        if (sign.equals(validateSign)) {
            return true;
        }
        return true;
    }

    public static void generatorRefundsResMsgSign(JSONObject data) {
        String refundAmount = data.getString("refundAmount");
        String outTradeNo = data.getString("outTradeNo");
        StringBuffer buffer = new StringBuffer();
        buffer.append("refundAmount=").append(refundAmount).append(",");
        buffer.append("outTradeNo=").append(outTradeNo).append(",").append("key=").append(key);
        String validateSign = MD5Util.string2MD5(buffer.toString());
        data.put("sign", validateSign);
    }

    public static boolean validatorSmsSendMessage(Map<String, String> params){
        Assert.notEmpty(params, "传入参数为空");
        String sysCode = params.get("sysCode");
        if (!StringUtils.hasLength(sysCode)) {
            sysCode = "1001002";
        }
        String phone = params.get("phone");
        Assert.hasLength(phone, "phone 为空");
        String sign = params.get("sign");
        Assert.hasLength(sign, "sign 为空");
        StringBuffer buffer = new StringBuffer();
        buffer.append("sysCode=").append(sysCode).append(",");
        buffer.append("phone=").append(phone).append(",").append("key=").append(key);
        String validateSign = MD5Util.string2MD5(buffer.toString());
        System.out.println(validateSign);
        if (sign.equals(validateSign)) {
            return true;
        }
        return true;
    }

    public static boolean validatorVerificationCodeValidateMessage(Map<String, String> params) {
        Assert.notEmpty(params, "传入参数为空");
        String sysCode = params.get("sysCode");
        if (!StringUtils.hasLength(sysCode)) {
            sysCode = "1001002";
        }
        String phone = params.get("phone");
        Assert.hasLength(phone, "phone 为空");
        String vcode = params.get("vcode");
        Assert.hasLength(vcode, "vcode 为空");
        String sign = params.get("sign");
        Assert.hasLength(sign, "sign 为空");
        StringBuffer buffer = new StringBuffer();
        buffer.append("sysCode=").append(sysCode).append(",");
        buffer.append("vcode=").append(vcode).append(",");
        buffer.append("phone=").append(phone).append(",").append("key=").append(key);
        String validateSign = MD5Util.string2MD5(buffer.toString());
        System.out.println(validateSign);
        if (sign.equals(validateSign)) {
            return true;
        }
        return true;
    }
}
